﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AMS.Chess.CommonLayer
{
    public abstract class AMove
    {
        // responsible for determinging where the piece can move

        protected AChessBoard _board;

        public AMove(AChessBoard board)
        {
            _board = board;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="movingPiece">The piece that is attempting to move.  Needed for friend/foe check.</param>
        /// <param name="startPosition"></param>
        /// <returns></returns>
        /// <remarks>Not requiring APiece at construction in case I decide to try to Singleton the AMove, and because APiece may get promoted.
        /// This Factory Method Pattern-like method enforces validation prior GeneratePossibleMoves</remarks>
        public List<ABoardPosition> GetPossibleMoves(AChessPiece movingPiece, ABoardPosition startPosition)
        {
            // verify valid position
            if (!_board.IsValidPosition(startPosition))
                throw new ArgumentOutOfRangeException("Invalid possible moves start position");

            // verify with board that this piece matches
            AChessPiece boardPiece = _board.GetPiece(startPosition);
            if (!movingPiece.Equals(boardPiece))
                throw new InvalidOperationException("board piece mismatch");

            return GenerateMoves(movingPiece, startPosition);
        }

        protected abstract List<ABoardPosition> GenerateMoves(AChessPiece movingPiece, ABoardPosition startPosition);
    }
}
